package com.cnzha.mr.friend;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.Arrays;

/**
 * @Author: zhaoxin
 * @email: zdkk@foxmail.com
 * @Date: 2020/4/6 23:29
 */
public class NewTwoFriendMapper extends Mapper<LongWritable, Text, Text, Text> {
    Text k = new Text();
    Text v = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        if (StringUtils.isEmpty(line)) {
            return;
        }
        String name = line.substring(0, 1);
        String[] split = line.substring(2).split(",");
        Arrays.sort(split);

        v.set(name);
        for (int i = 0; i < split.length; i++) {
            for (int j = i + 1; j < split.length; j++) {
                k.set(split[i] + "-" + split[j]);
                context.write(k, v);
            }
        }

    }
}
